const STORAGE_TYPE = {
  LOCAL: "localStorage",
  SESSION: "sessionStorage",
  COOKIE: "cookie",
};

const DEFAULT_STORAGE = STORAGE_TYPE.LOCAL;
let currentStorage = DEFAULT_STORAGE;

/**
 * 设置缓存存储类型
 * @param {'localStorage'|'sessionStorage'|'cookie'} type
 */
export function setStorageType(type) {
  if (Object.values(STORAGE_TYPE).includes(type)) {
    currentStorage = type;
  } else {
    console.warn(
      `Invalid storage type: ${type}. Using default: ${DEFAULT_STORAGE}`
    );
    currentStorage = DEFAULT_STORAGE;
  }
}

/**
 * 获取缓存值
 * @param {string} key
 * @param {*} [defaultValue] - 当获取不到时的默认值
 * @returns {string|null|*}
 */
export function getCache(key, defaultValue = null) {
  try {
    let value;

    switch (currentStorage) {
      case STORAGE_TYPE.LOCAL:
        value = localStorage.getItem(key);
        break;
      case STORAGE_TYPE.SESSION:
        value = sessionStorage.getItem(key);
        break;
      case STORAGE_TYPE.COOKIE:
        value = cookie.get(key);
        break;
      default:
        value = localStorage.getItem(key);
    }

    return value ?? defaultValue;
  } catch (error) {
    console.error(`Failed to get cache for key "${key}":`, error);
    return defaultValue;
  }
}

/**
 * 设置缓存值
 * @param {string} key
 * @param {*} value
 * @param {Object} [options] - 额外选项
 * @param {number} [options.expires] - 过期时间(毫秒)，仅对cookie有效
 * @returns {boolean} 是否设置成功
 */
export function setCache(key, value, options = {}) {
  try {
    const stringValue =
      typeof value === "string" ? value : JSON.stringify(value);

    switch (currentStorage) {
      case STORAGE_TYPE.LOCAL:
        localStorage.setItem(key, stringValue);
        break;
      case STORAGE_TYPE.SESSION:
        sessionStorage.setItem(key, stringValue);
        break;
      case STORAGE_TYPE.COOKIE:
        cookie.set(key, stringValue, {
          expires: options.expires
            ? new Date(Date.now() + options.expires)
            : undefined,
        });
        break;
      default:
        localStorage.setItem(key, stringValue);
    }

    return true;
  } catch (error) {
    console.error(`Failed to set cache for key "${key}":`, error);
    return false;
  }
}

/**
 * 移除缓存值
 * @param {string} key
 * @returns {boolean} 是否移除成功
 */
export function removeCache(key) {
  try {
    switch (currentStorage) {
      case STORAGE_TYPE.LOCAL:
        localStorage.removeItem(key);
        break;
      case STORAGE_TYPE.SESSION:
        sessionStorage.removeItem(key); // 改为removeItem而不是clear
        break;
      case STORAGE_TYPE.COOKIE:
        cookie.remove(key);
        break;
      default:
        localStorage.removeItem(key);
    }

    return true;
  } catch (error) {
    console.error(`Failed to remove cache for key "${key}":`, error);
    return false;
  }
}

/**
 * 清空当前存储的所有缓存
 * @returns {boolean} 是否清空成功
 */
export function clearAllCache() {
  try {
    switch (currentStorage) {
      case STORAGE_TYPE.LOCAL:
        localStorage.clear();
        break;
      case STORAGE_TYPE.SESSION:
        sessionStorage.clear();
        break;
      case STORAGE_TYPE.COOKIE:
        Object.keys(cookie.get()).forEach((key) => cookie.remove(key));
        break;
      default:
        localStorage.clear();
    }

    return true;
  } catch (error) {
    console.error("Failed to clear all cache:", error);
    return false;
  }
}
